package com.hp.bon.sgw.wh.utils;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hp.bon.sgw.Constants;
import com.hp.bon.sgw.wh.util.DateUtil;
import com.hp.ocs.diameter.message.AVP;
import com.hp.ocs.diameter.message.AVP_Grouped;
import com.hp.ocs.diameter.message.AVP_Time;
import com.hp.ocs.diameter.message.AVP_UTF8String;
import com.hp.ocs.diameter.message.InvalidAVPLengthException;
import com.hp.ocs.diameter.message.Message;

/**
 * 
 * 说   明：SGW手机直充 & 订单查询 接口处理类 
 * 描   述：SGW手机直充 & 订单查询 接口处理类 
 * @author C.J
 * @date 2017年12月5日 上午9:38:27
 */
public class TmallPayUtil {
	private static final Logger _log = LoggerFactory.getLogger(TmallPayUtil.class);
	
	public static Element getServiceInfoEle(String customer,String bankCode,String tbOrderNo,String payment_Amount,String sum,String flag){
		_log.info("=======进入flag鉴定DCC请求参数=====flag:"+flag);
		if("0".equals(flag)) {
			Element serviceInfo = DocumentHelper.createElement("Service_Information");
			// Acc_Nbr
			Element element = serviceInfo.addElement("Acc_Nbr");
			element.setText(customer);
			// Destination_Attr
			element = serviceInfo.addElement("DestinationAttr");
			element.setText("2");
			// Area_Code
			element = serviceInfo.addElement("Area_Code");
			element.setText("027");
			// Bank_code
			element = serviceInfo.addElement("Bank_code");
			element.setText(bankCode);
			// Payment_Serial_Nbr
			element = serviceInfo.addElement("Payment_Serial_Nbr");
			element.setText(tbOrderNo);
			// Payment_Amount
			element = serviceInfo.addElement("Payment_Amount");
			element.setText(payment_Amount);
			// RequestAmount
			element = serviceInfo.addElement("RequestAmount");
			element.setText(sum.replace(".", ""));
			return serviceInfo;
		}else {
			Element serviceInfo = DocumentHelper.createElement("Service_Information");
			Element element = serviceInfo.addElement("Channel_Id");
			element.setText("200");
			element = serviceInfo.addElement("DestinationAttr");
			element.setText("2");
			element = serviceInfo.addElement("Area_Code");
			element.setText("027");
			element = serviceInfo.addElement("Destination_Id");
			element.setText(customer);
			element = serviceInfo.addElement("Destination_Id_Type");
			element.setText("2");
			return serviceInfo;
		}
		
	}

	public static String[] getAvpsFromMessage(Message answer){
		String[] avpsArr = new String[5];
		String billSerialNbr = "";
		String operTime = "";
	    AVP avp = answer.find(Constants.DIAMETER_AVP_SERVICE_INFOMATION,Constants.DIAMETER_VENDOR_3GPP);
	    AVP_Grouped avpServiceInfomation;
		try {
			avpServiceInfomation = new AVP_Grouped(avp);
		    AVP avpBillSerialNbr = avpServiceInfomation.find(80133,Constants.DIAMETER_VENDOR_CT);
		    if (avpBillSerialNbr != null){
		    	billSerialNbr = new AVP_UTF8String(avpBillSerialNbr).queryValue();
		    	avpsArr[0] = billSerialNbr;
		    }
		    AVP avpOperTime = avpServiceInfomation.find(5073,Constants.DIAMETER_VENDOR_CT);
			if (avpOperTime != null){
				operTime = new AVP_UTF8String(avpOperTime).queryValue();
				avpsArr[1] = operTime;
				//avpsArr[1] = DateUtil.dateTimeToStr(new AVP_Time(avpOperTime).queryDate());
				_log.info("======DateUtil1:"+DateUtil.dateTimeToStr(new AVP_Time(avpOperTime).queryDate()));
				_log.info("======DateUtil2:"+new AVP_Time(avpOperTime).queryValueAsString());
			}
			AVP Latn_Id = avpServiceInfomation.find(80411,Constants.DIAMETER_VENDOR_CT);
			if (Latn_Id != null){
				avpsArr[2] = new AVP_UTF8String(Latn_Id).queryValue();
			}
			AVP Area_Code = avpServiceInfomation.find(50207,Constants.DIAMETER_VENDOR_CT);
			if (Area_Code != null){
				avpsArr[3] = new AVP_UTF8String(Area_Code).queryValue();
			}
			AVP Paid_Time = avpServiceInfomation.find(80239,Constants.DIAMETER_VENDOR_CT);
			if (Paid_Time != null){
				avpsArr[4]  = DateUtil.dateTimeToStr(new AVP_Time(Paid_Time).queryDate());
				_log.info("======DateUtil3:"+DateUtil.dateTimeToStr(new AVP_Time(Paid_Time).queryDate()));
				_log.info("======DateUtil4:"+new AVP_Time(Paid_Time).queryValueAsString());
			}
			
		} catch (InvalidAVPLengthException e) {
			avpsArr = null;
			_log.error(" getAvpsFromMessage is error when Ne response cca message");
		}
		return avpsArr;
	}
	
	
}
